//
//  RSWDataOperation+WorkSummary.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/22.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#import "RSWDataOperation+WorkSummary.h"
#import "RSWRESTService+WorkSummary.h"

@implementation RSWDataOperation (WorkSummary)

-(void)addWorkSummary:(WorkSummaryInfo*)info completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
      {
          [self progressViewBeginWithTip:@"提交中，请稍候"];
          
          XLGResult *r = nil;
          NSString *token = [XLGContext context].m_token;
          RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
          if(ser != nil)
          {
              r = [ser addWorkSummary:info token:token];
              [self detachDataOperationManager:@[ser]];
          }
          
          [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
           {
               completion(nResult,[output boolValue],error);
           }];
          
          [self progressViewEnd];
      }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}

-(void)getMyWorkSummarySourceType:(WorkSummarySourceType)sourceType pageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize completion:(void(^)(NSInteger nResult,NSArray *list,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
      {
          [self pullRefreshViewBegin];
          
          XLGResult *r = nil;
          RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
          if(ser != nil)
          {
              NSString *filter = @"";
              NSString *token = [XLGContext context].m_token;
              
              if(sourceType == MyWorkSummarySource)
              {
                  NSInteger userNumber = [MyInfo myInfo].context.m_nUsernumber;
              
                  r = [ser getMyWorkSummaryFilter:filter token:token pageIndex:pageIndex pageSize:pageSize userNumber:userNumber];
              }
              else if(sourceType == LeaderSummarySource)
              {
                  r = [ser getLeaderWorkSummaryFilter:filter token:token pageIndex:pageIndex pageSize:pageSize];
              }
              [self detachDataOperationManager:@[ser]];
          }
          
          [self requestResult:r.outResult output:r.outObj error:r.outError completion:completion];
          
          [self pullRefreshViewEnd];
      }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,nil,nil);
}

-(void)getWorkSummaryCommentById:(NSString*)workSummaryId isView:(BOOL)isView completion:(void(^)(NSInteger nResult,NSArray *list,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
      {
          [self pullRefreshViewBegin];
          
          XLGResult *r = nil;
          RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
          if(ser != nil)
          {
              NSInteger pageIndex = 1;
              NSInteger pageSize = 10000;
              NSString *token = [XLGContext context].m_token;
              
              r = [ser getWorkSummaryCommentById:workSummaryId token:token pageIndex:pageIndex pageSize:pageSize];
              
              if(r.outResult == YES)
              {
                  if(isView == YES)
                  {
                      [ser viewWorkSummary:workSummaryId token:token];
                  }
              }
              [self detachDataOperationManager:@[ser]];
          }
          
          [self requestResult:r.outResult output:r.outObj error:r.outError completion:completion];
          
          [self pullRefreshViewEnd];
      }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,nil,nil);
}

NSString *const RSWDataOperationWorkSummaryModifyNotification = @"RSWDataOperationWorkSummaryModifyNotification";
-(void)modifyWorkSummary:(WorkSummaryInfo*)info completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
      {
          [self progressViewBeginWithTip:@"提交中，请稍候"];
          
          XLGResult *r = nil;
          NSString *token = [XLGContext context].m_token;
          RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
          if(ser != nil)
          {
              r = [ser modifyWorkSummary:info token:token];
              [self detachDataOperationManager:@[ser]];
              
              if(r.outResult == YES)
              {
                  [NSNotificationCenter postNotification:RSWDataOperationWorkSummaryModifyNotification userInfo:@{@"1":info}];
              }
          }
          
          [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
           {
               completion(nResult,[output boolValue],error);
           }];
          
          [self progressViewEnd];
      }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}

NSString *const RSWDataOperationWorkSummaryDeleteNotification = @"RSWDataOperationWorkSummaryDeleteNotification";
-(void)deleteWorkSummary:(WorkSummaryInfo*)info completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
      {
          [self progressViewBeginWithTip:@"提交中，请稍候"];
          
          XLGResult *r = nil;
          NSString *token = [XLGContext context].m_token;
          RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
          if(ser != nil)
          {
              r = [ser deleteWorkSummary:info token:token];
              [self detachDataOperationManager:@[ser]];
              
              if(r.outResult == YES)
              {
                  [NSNotificationCenter postNotification:RSWDataOperationWorkSummaryDeleteNotification userInfo:@{@"1":info}];
              }
          }
          
          [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
           {
               completion(nResult,[output boolValue],error);
           }];
          
          [self progressViewEnd];
      }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}

NSString *const RSWDataOperationWorkSummaryCommentNotification = @"RSWDataOperationWorkSummaryCommentNotification";
-(void)commentWorkSummary:(WorkSummaryInfo*)info commentContent:(NSString*)commentContent completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
      {
          [self progressViewBeginWithTip:@"提交中，请稍候"];
          
          XLGResult *r = nil;
          NSString *token = [XLGContext context].m_token;
          RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
          if(ser != nil)
          {
              r = [ser commentWorkSummary:info token:token commentContent:commentContent];
              [self detachDataOperationManager:@[ser]];
              
              if(r.outResult == YES)
              {   
                  [NSNotificationCenter postNotification:RSWDataOperationWorkSummaryCommentNotification userInfo:@{@"1":info}];
              }
          }
          
          [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
           {
               completion(nResult,[output boolValue],error);
           }];
          
          [self progressViewEnd];
      }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}
@end
